package com.xmut.bs.handaccount.activity;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;

import com.xmut.bs.handaccount.GlideApp;
import com.xmut.bs.handaccount.R;
import com.xmut.bs.handaccount.base.CommonListActivity;
import com.xmut.bs.handaccount.bmob.Comment;
import com.xmut.bs.handaccount.bmob.Goods;
import com.xmut.bs.handaccount.util.GlideCircleTransform;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

import static com.xmut.bs.handaccount.base.Constants.PARAM_GOODS;

/**
 * Description
 * Created by Dollkey on 2018/4/17.
 */

public class CommentActivity extends CommonListActivity<Comment> {

    private Goods mGoods;

    @Override
    protected String getSubTitle() {
        return "全部评论";
    }

    @Override
    protected RecyclerView.ItemDecoration getItemDecoration() {
        return null;
    }

    @NonNull
    @Override
    protected RecyclerView.LayoutManager getRvLayoutManager() {
        return new LinearLayoutManager(mContext);
    }

    @Override
    protected RecyclerView.Adapter getAdapter(List<Comment> comList) {
        return new CommonAdapter<Comment>(mContext, R.layout.item_comment,comList) {
            @Override
            protected void convert(ViewHolder holder, Comment comment, int position) {
                holder.setText(R.id.time, comment.getCreatedAt());
                holder.setText(R.id.content, comment.getComment());
                holder.setText(R.id.username, comment.getUser().getNickName());
                ImageView avatarImg = holder.getView(R.id.avatar_img);

                GlideApp.with(avatarImg).load(comment.getUser().getAvatar())
                        .placeholder(R.mipmap.ic_head_img).error(R.mipmap.ic_head_img)
                        .transform(new GlideCircleTransform(mContext)).override(60, 60).into(avatarImg);
            }
        };
    }

    @Override
    public void getData(int pageNo, int pageSize) {
        BmobQuery<Comment> query = new BmobQuery<>();
        query.addWhereEqualTo("goods", mGoods);
        query.addWhereEqualTo("article", null);
        query.order("-updatedAt");
        query.include("user");// 希望在查询帖子信息的同时也把发布人的信息查询出来
        query.findObjects(new FindListener<Comment>() {
            @Override
            public void done(List<Comment> list, BmobException e) {
                if (e == null) {
                    showList(list);
                } else {
                    showError("请求失败：" + e.getMessage());
                }
            }
        });
    }

    @Override
    protected void initParams(Intent intent) {
        mGoods = (Goods) intent.getSerializableExtra(PARAM_GOODS);
    }
}
